package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowProcessInstanceTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.FlowTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessInstanceStatisticsListVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessInstanceStatisticsQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessSummaryQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.ProcessSummaryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.TaskStatisticsListVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.statistics.TaskStatisticsQueryVo;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lilh
 * @date 2019-08-26 13:39
 */
@RestController
@RequestMapping("/statistics")
public class FlowStatisticsController {

    @Autowired
    private FlowStatisticsService flowStatisticsService;


    /**
     * 任务明细统计列表
     */
    @PostMapping("/task/list")
    public ResponseResult listTask(@RequestBody TaskStatisticsQueryVo taskStatisticsQueryVo) {
        PageInfo<FlowTaskTraceVo> pageInfos = new PageInfo<>(flowStatisticsService.listTask(taskStatisticsQueryVo));
        return ResponseResult.success(pageInfos.getList().stream().map(TaskStatisticsListVo::new).collect(Collectors.toList())).setTotalCount((int) pageInfos.getTotal())
                .setPageNum(pageInfos.getPageNum()).setPageSize(pageInfos.getPageSize());
    }


    /**
     * 流程实例明细列表
     */
    @PostMapping("/processInstance/list")
    public ResponseResult listProcessInstance(@RequestBody ProcessInstanceStatisticsQueryVo processInstanceStatisticsQueryVo) {
        PageInfo<FlowProcessInstanceTraceVo> pageInfos = new PageInfo<>(flowStatisticsService.listProcessInstance(processInstanceStatisticsQueryVo));
        return ResponseResult.success(pageInfos.getList().stream().map(ProcessInstanceStatisticsListVo::new).collect(Collectors.toList())).setTotalCount((int) pageInfos.getTotal())
                .setPageNum(pageInfos.getPageNum()).setPageSize(pageInfos.getPageSize());
    }

    /**
     * 任务统计
     */
    @PostMapping("/task/summary")
    public ResponseResult taskSummary(@RequestBody ProcessSummaryQueryVo processSummaryQueryVo) {
        PageInfo<ProcessSummaryVo> pageInfos = new PageInfo<>(flowStatisticsService.taskSummary(processSummaryQueryVo));
        return ResponseResult.success(pageInfos.getList()).setTotalCount((int) pageInfos.getTotal())
                .setPageNum(pageInfos.getPageNum()).setPageSize(pageInfos.getPageSize());
    }


    /**
     * 实例统计
     */
    @PostMapping("/processInstance/summary")
    public ResponseResult processInstanceSummary(@RequestBody ProcessSummaryQueryVo processSummaryQueryVo) {
        PageInfo<ProcessSummaryVo> pageInfos = new PageInfo<>(flowStatisticsService.processInstanceSummary(processSummaryQueryVo));
        return ResponseResult.success(pageInfos.getList()).setTotalCount((int) pageInfos.getTotal())
                .setPageNum(pageInfos.getPageNum()).setPageSize(pageInfos.getPageSize());
    }
}

